import axios from "axios";
import { Promise } from "core-js";

// 创建axios实例
let instance = axios.create({
    baseURL: 'http://192.168.1.28:5238',
    timeout: 5000
})

// 请求拦截器
instance.interceptors.request.use(
    function (config) {
        // config.headers.token = ''
        return config
    },
    function (error) {
        return Promise.reject(error) //错误请求做些什么
    }
)

// 响应拦截器
instance.interceptors.response.use(
    function (response) {
        return response.data
    },
    function (error) {
        return Promise.reject(error)
    }
)

export default function (method, url, data = null) {
    method = method.toLowerCase()
    if (method === 'post') {
        return instance.post(url, data)
    } else if (method === 'get') {
        return instance.get(url, { params: data })
    } else if (method === 'delete') {
        return instance.delete(url, { params: data })
    } else if (method === 'put') {
        return instance.put(url, data)
    } else {
        console.error('未知的method' + method)
        return false
    }
}

